/**
 * @author ashok
 * Chatter Notifier App.
 *
 * Test Class covering chatter Notifier code. 
 *
 * Copyright:  Copyright 2010
 * 
 * This file is part of Chatter Notifier. 
 * 
 * Chatter Notifier is free software: you can redistribute it and/or modify
 * it under the terms of the New BSD license.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */
@isTest
private class TestChatterNotifier {

    static testMethod void chatterProfileControllerTest() {        
        ChatterProfileController controller = new ChatterProfileController();
        User user = createTestUser('aKumar', 'ashok-notifier@chatter.com', 'Kumar');
        insert user;
        
        // creating EntitySubscription entry.
        createEntitySubscription(user);
        
        Apexpages.currentPage().getParameters().put('userId', user.Id);
                
        controller.init();
        controller.editChatterProfile();
        System.assert(controller.showEditForm);
        
        controller.chatterProfile.Birthday__c = System.today();
        controller.chatterProfile.Birthday_display__c = ChatterUtil.SHOW_FULL_DATE;
        controller.chatterProfile.Relationship_Status__c = 'Widowed';
        controller.chatterProfile.Anniversary__c = System.today();
        
        controller.save();
        
        controller.editChatterProfile();
        controller.cancel();
        System.assert(!controller.showEditForm);
        
        controller.save();
        
        controller.editNotificationSetting();
        System.assert(controller.showSettingForm);
        
        controller.cancelNotificationSetting();
        System.assert(!controller.showSettingForm);
        
        controller.chatterProfile.Birthday_Notification__c = true;
        controller.chatterProfile.Anniversary_Notification__c = true;
        controller.saveSetting();     
        
        User user2 = createTestUser('MSharma', 'Mohit-Notifier@chatter.com', 'Sharma');
        Chatter_Profile__c chatterProfile = new Chatter_Profile__c(Name = user2.Name, User__c = user2.Id, Birthday_Notification__c = true, Anniversary_Notification__c = true,
    					  Birthday__c = System.today().addDays(1), Anniversary__c = System.today().addDays(1));
    		
    	insert chatterProfile;
    	System.debug('dfate -------'+ chatterProfile.Birthday__c);	  
        // Calling ScheduledChatterNotification code for execution.
        ScheduledChatterNotification schedulerNotifier = new ScheduledChatterNotification();
     	schedulerNotifier.execute(null);    
    }
    
    static testMethod void chatterUtilTest() {
    	Date dt = System.today().toStartOfWeek();
    	
    	String dayStr1 = ChatterUtil.getDayOfWeek(dt);    
    	System.assertEquals('Sunday', dayStr1);	
    	ChatterUtil.getDayOfWeek(dt.addDays(1));
    	ChatterUtil.getDayOfWeek(dt.addDays(2));
    	ChatterUtil.getDayOfWeek(dt.addDays(3));
    	ChatterUtil.getDayOfWeek(dt.addDays(4));
    	ChatterUtil.getDayOfWeek(dt.addDays(5));
    	String dayStr2 = ChatterUtil.getDayOfWeek(dt.addDays(6));
    	System.assertEquals('Saturday', dayStr2);
    	
    	Date dtM = Date.newInstance(2010, 01, 01);
    	
    	String strMonth = ChatterUtil.getMonthAsString(dtM);
    	System.assertEquals('January', strMonth);
    	ChatterUtil.getMonthAsString(dtM.addMonths(1));
    	ChatterUtil.getMonthAsString(dtM.addMonths(2));
    	ChatterUtil.getMonthAsString(dtM.addMonths(3));
    	ChatterUtil.getMonthAsString(dtM.addMonths(4));
    	ChatterUtil.getMonthAsString(dtM.addMonths(5));
    	ChatterUtil.getMonthAsString(dtM.addMonths(6));
    	ChatterUtil.getMonthAsString(dtM.addMonths(7));
    	ChatterUtil.getMonthAsString(dtM.addMonths(8));
    	ChatterUtil.getMonthAsString(dtM.addMonths(9));
    	ChatterUtil.getMonthAsString(dtM.addMonths(10)); 
    	String strMonth2 = ChatterUtil.getMonthAsString(dtM.addMonths(11));
    	System.assert(strMonth2 == 'December');
    }
    
    // Method for creating test user.
    public static User createTestUser(String alias, String username, String lname) {
    	Profile p = [SELECT Id FROM profile WHERE name='System Administrator'];
        User mock = new User(alias = alias, email='ashok_itcs@rediffmail.com',
                emailencodingkey='UTF-8', lastname= lname, languagelocalekey='en_US',
                localesidkey='en_US', profileid = p.Id,
                timezonesidkey='America/Los_Angeles', username= username);
        return mock;    
    }
    
    public static void createEntitySubscription(User parentUser) {
    	User subscriberUser = createTestUser('MSharma', 'Mohit-Notifier@chatter.com', 'Sharma');
    	insert subscriberUser;
    	EntitySubscription es1 = new EntitySubscription(parentid = parentUser.id, subscriberid = subscriberUser.Id);
    	
    	EntitySubscription es2 = new EntitySubscription(parentid = subscriberUser.Id, subscriberid = parentUser.id);	
    	List<EntitySubscription> esList = new List<EntitySubscription>{es1, es2};
    	insert esList;	 					
    }
}